<template>
  <a-row :gutter="24">
    <a-col :md="4" :sm="24">
      <a-card :bordered="false" :loading="treeLoading">
        <a-directory-tree
          :showIcon="false"
          :treeData="treeList"
          v-if="treeList.length"
          @select="handleClick"
          :defaultExpandAll="true"
        />
      </a-card>
    </a-col>

    <a-col :md="19" :sm="24">
      <a-card :bordered="false" v-show="gatewayId!==0">
        <div class="table-page-search-wrapper">
          <div class="table-operator" style="text-align: left">
            <a-button
              type="primary"
              class="btn_add"
              @click="$refs.addForm.add(gatewayId,stationId)"
              icon="plus">新增
            </a-button>
          </div>

          <s-table
            ref="table"
            size="middle"
            :columns="columns"
            :data="loadData"
            :alert="true"
            :rowKey="(record) => record.id"
          >
            <span slot="thresholdValue" slot-scope="text, record">
              <a @click="thresholdItemSet(record)">{{ thresholdItem(text) }}</a>
            </span>

            <span slot="status" slot-scope="text">
              {{ getStatus(text) }}
            </span>

            <span slot="action" slot-scope="text, record">
              <a @click="$refs.editForm.edit(record,stationId)" class="btn_edit">编辑</a>
              <a-divider type="vertical"/>
              <a-popconfirm placement="topRight" title="确认删除？" class="btn_del" @confirm="() => del(record)">
                <a>删除</a>
              </a-popconfirm>
            </span>
          </s-table>

          <add-form ref="addForm" @ok="handleOk"/>
          <edit-form ref="editForm" @ok="handleOk"/>
          <threshold-item-edit ref="thresholdItemEdit" @ok="handleOk"/>
          <threshold-item-add ref="thresholdItemAdd" @ok="handleOk"/>
        </div>
      </a-card>
    </a-col>
  </a-row>
</template>

<script>
  import { treeList } from '@/api/modular/main/bizgateway/bizGatewayManage'
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/bizgatewaychannel/bizGatewayChannelManage'
  import addForm from '../bizgateway/bizgatewaychannel/addForm'
  import editForm from '../bizgateway/bizgatewaychannel/editForm'
  import thresholdItemEdit from '../bizthreholditem/editForm'
  import thresholdItemAdd from '../bizthreholditem/addForm'
  import { threholdItemByChannelAndGatewayId } from '@/api/modular/main/bizthreholditem/bizThreholdItemManage'

  export default {
    components: {

      STable,
      addForm,
      editForm,
      thresholdItemEdit,
      thresholdItemAdd
    },
    props: {
      stationId: {
        type: Number,
        require: false
      }
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 14 }
        },
        advanced: false, // 高级搜索 展开/关闭
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        treeList: [],
        treeLoading: true,
        queryParam: {},
        gatewayId: 0,

        columns: [ // 表头
          {
            title: '通道',
            dataIndex: 'channel'
          },
          {
            title: '阈值',
            dataIndex: 'thresholdValue',
            scopedSlots: { customRender: 'thresholdValue' }
          },
          {
            title: '安装位置',
            dataIndex: 'installLocation'
          },
          {
            title: '设备名称',
            dataIndex: 'deviceName'
          },
          {
            title: '状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '产品厂商',
            dataIndex: 'productVendor'
          },
          {
            title: '产品型号',
            dataIndex: 'productModel'
          },
          {
            title: '创建日期',
            dataIndex: 'createdAt'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },

    created () {
      this.queryParam.stationId = this.stationId
      this.getGatewayTree()
    },

    methods: {
      getGatewayTree () {
        treeList(Object.assign(this.queryParam)).then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.treeList = res.data
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      handleClick (e) {
        if (e.length > 0) {
          this.gatewayId = e.toString()
          this.queryParam.gatewayId = e.toString()
          try {
            this.$refs.table.refresh()
          } catch (e) {
            // 首次进入界面，因表格加载顺序，会抛异常，我们不予理会
          }
        }
      },
      // 打开此页面首先加载此方法
      toIndex (record) {
        this.visible = true
        this.gatewayId = record.id
        this.stationId = record.stationId
      },
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      handleCancel () {
        this.queryParam = {}
        this.visible = false
      },
      thresholdItem (text) {
        if (text) {
          return text
        } else {
          return '无'
        }
      },
      getStatus (text) {
        if (text === 0) {
          return '无声'
        } else if (text === 1) {
          return '正常'
        } else if (text === 2) {
          return '过载'
        } else {
          return '未知'
        }
      },
      thresholdItemSet (record) {
        const param = {
          gatewayChannel: record.channel,
          gatewayId: record.gatewayId
        }
        threholdItemByChannelAndGatewayId(param).then(res => {
          if (res) {
            this.$refs.thresholdItemEdit.edit(record)
          } else {
            this.$refs.thresholdItemAdd.add(param)
          }
        })
      }
    }
  }
</script>

<style lang="less">
  @import '../~@/assets/css/list.less';

  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }
</style>
